package com.rimi.service.impl;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.payment.common.models.AlipayTradeRefundResponse;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;
import com.alipay.easysdk.payment.wap.models.AlipayTradeWapPayResponse;
import com.rimi.commons.CommonResult;
import com.rimi.model.vo.PaymentVo;
import com.rimi.model.vo.RefundVo;
import com.rimi.service.AlipayService;
import com.rimi.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;

@Service
public class AlipayServiceImpl implements AlipayService {

    //支付成功后要跳转的页面
    @Value("${alipay.returnUrl}")
    private String returnUrl;

    //取消支付跳转地址
    @Value("${alipay.errorUrl}")
    private String errorUrl;

    @Autowired
    OrderService orderService;

    /**
     * 电脑网页支付
     * @param
     * @return
     */
    @Override
    public String pagePay(PaymentVo paymentVo) {
        //获取商品名
        String goodsName = paymentVo.getGoodsName();
        //获取订单号
        Long orderNumber = paymentVo.getOrderNumber();
        //获取单价
        Double goodsPrice = paymentVo.getGoodsPrice();
        //获取数量
        Integer goodsQuantity = paymentVo.getGoodsQuantity();
        //计算总价
        Double totalAmount = goodsPrice * goodsQuantity;
        //支付方式
        try {
            AlipayTradePagePayResponse response = Factory
                    .Payment
                    //page() 为电脑网页支付
                    .Page()
                    .pay(goodsName, orderNumber + "", totalAmount + "", returnUrl);

            return response.body;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "支付失败";
    }

    /**
     * 手机网页支付
     * @param paymentVo
     * @return
     */
    @Override
    public String wapPay(PaymentVo paymentVo) {
        //获取商品名
        String goodsName = paymentVo.getGoodsName();
        //获取订单号
        Long orderNumber = paymentVo.getOrderNumber();
        //获取单价
        Double goodsPrice = paymentVo.getGoodsPrice();
        //获取数量
        Integer goodsQuantity = paymentVo.getGoodsQuantity();
        //计算总价
        Double totalAmount = goodsPrice * goodsQuantity;
        //支付方式 war() 为手机网页支付
        try {
            AlipayTradeWapPayResponse response = Factory
                    .Payment
                    // wap()为手机网页支付
                    .Wap()
                    .pay(goodsName, orderNumber + "", totalAmount + "", errorUrl, returnUrl);
            return response.body;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "支付失败";
    }

    /**
     * 退款
     * @param refundVo
     * @return
     */
    @Override
    public String refund(RefundVo refundVo) {
        //获取商品名
        String goodsName = refundVo.getGoodsName();
        //获取订单号
        Long orderNumber = refundVo.getOrderNumber();
        //获取单价
        Double goodsPrice = refundVo.getGoodsPrice();
        //获取数量
        Integer goodsQuantity = refundVo.getGoodsQuantity();
        //计算总价
        Double totalAmount = goodsPrice * goodsQuantity;
        //获取支付时间
        Date paymentTime = refundVo.getPaymentTime();
        //获取退款状态
        String refundStatus = refundVo.getRefundStatus();
        //当前时间
        Date currentTime = new Date();
        //得到日历
        Calendar calendar = Calendar.getInstance();
        // 把当前时间赋给日历
        calendar.setTime(currentTime);
        // 设置为7天前
        calendar.add(Calendar.DAY_OF_MONTH, -7);
        // 得到7天前的时间
        Date before7days = calendar.getTime();
        //判断有无当比订单
        if (orderNumber == null){
            return "没有查询到这笔订单";
        }
        //判断有没有支付
        if (paymentTime == null){
            return "还没有支付的订单不能退款";
        }
        //判断退款状态
        if ("退款中".equals(refundStatus)){
            return "商品正在退款中";
        }

        if ("退款成功".equals(refundStatus)){
            return "已退款订单不能再次退款";
        }

        //判断退款金额
        if (refundVo.getRefundPrice()>totalAmount){
            return "退款金额不能大于支付金额";
        }
        //判断时间
        if (before7days.getTime() > paymentTime.getTime()) {
            // 满足大于7天的用户不再退货的金额
            return "亲,收货已超过七天,不能退款了!";
        }
        if (before7days.getTime() < paymentTime.getTime()) {
            // 在7天内的订单金额没有入账
            // 修改订单退款状态为“退款中”
            orderService.updateRefundOrderStatusForDuring(refundVo);
            //延迟30s时间退款
            try {
                Thread.sleep(30000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //进行退款操作
            try {
                AlipayTradeRefundResponse response = Factory.Payment.Common().refund(orderNumber+"", totalAmount+"");
                if (response.msg.equals("Success")) {
                    // 修改订单退款状态为“退款成功”
                    int success = orderService.updateRefundOrderStatusForSuccess(refundVo);
                    if (success > 0){
                        return "退款成功";
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        // 修改订单退款状态为“退款失败”
        orderService.updateRefundOrderStatusForFail(refundVo);
        return "退款失败";
    }
}
